package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Custom;
import com.aaa.tmr.service.CustomService;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * (Custom)表控制层
 *
 * @author makejava
 * @since 2021-03-24 16:23:52
 */
@Controller
@RequestMapping("custom")
public class CustomController {
    /**
     * 服务对象
     */
    @Resource
    private CustomService customService;
    @Resource
    private HttpServletResponse response;

    @ResponseBody
    @RequestMapping("exportCustom")
    public Result exportCustom(Custom custom){
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0L,customService.exportCustom(custom));
    }

    @ResponseBody
    @RequestMapping("importCustom")
    public Result importCustom(@RequestParam MultipartFile file) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                0L,customService.importCustom(file));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toCustom")
    public String toCustom() {
        return "back/custom-page";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Custom custom) {
        PageInfo pageInfo = customService.queryAll(page, limit, custom);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param custom 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Custom custom) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.customService.insert(custom));
    }

    /**
     * 修改数据
     *
     * @param custom 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Custom custom) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.customService.update(custom));
    }

    /**
     * 通过主键删除数据
     *
     * @param customId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer customId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0L,
                this.customService.deleteById(customId));
    }
}
